﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using FlexCell;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;
using libxl;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.Office.Interop.Excel;
using Microsoft.Win32;
using YJKPresentation.Utils;

namespace YJKExcelTableUtil
{
	public class GridToolExportImportExcel
	{
		public GridToolExportImportExcel(bool bXlsx)
		{
			if (bXlsx)
			{
				this.m_book = this.m_xmlBook;
			}
			else
			{
				this.m_book = this.m_binBook;
			}
			this.m_book.setKey("Beijing YArchitech Tongxing Technology Co.,Ltd", "windows-282b2c0f08cbe70966b4616ea0i8d7n0");
			this.GetXYDpi(ref this.m_dpiX, ref this.m_dpiY);
		}

		private void GetXYDpi(ref double Xdpi, ref double Ydpi)
		{
			using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
			{
				Xdpi = (double)graphics.DpiX;
				Ydpi = (double)graphics.DpiY;
			}
		}

		public bool ExportExcel(Grid grid, string FilePath)
		{
			if (grid == null || string.IsNullOrEmpty(FilePath))
			{
				return false;
			}
			if (this.DataFromGridToExcel(grid))
			{
				this.m_book.save(FilePath);
				Process.Start(FilePath);
				return true;
			}
			return false;
		}

		public bool ExportExcel(Dictionary<List<string>, Grid> grids, string FilePath)
		{
			if (grids == null || grids.Count == 0 || string.IsNullOrEmpty(FilePath))
			{
				return false;
			}
			bool flag = true;
			List<List<string>> columnsList = new List<List<string>>();
			foreach (KeyValuePair<List<string>, Grid> keyValuePair in grids)
			{
				if (!this.DataFromGridToExcel(keyValuePair.Value, keyValuePair.Key, columnsList))
				{
					flag = false;
					break;
				}
			}
			if (flag)
			{
				this.m_book.save(FilePath);
				if (this.ExistsRegedit() != 0)
				{
					this.AddDropDownList(FilePath, columnsList);
				}
				return true;
			}
			return false;
		}

		public int ExistsRegedit()
		{
			int num = 0;
			RegistryKey localMachine = Registry.LocalMachine;
			RegistryKey registryKey = localMachine.OpenSubKey("SOFTWARE\\Microsoft\\Office\\12.0\\Excel\\InstallRoot\\");
			RegistryKey registryKey2 = localMachine.OpenSubKey("SOFTWARE\\Microsoft\\Office\\14.0\\Excel\\InstallRoot\\");
			RegistryKey registryKey3 = localMachine.OpenSubKey("SOFTWARE\\Microsoft\\Office\\15.0\\Excel\\InstallRoot\\");
			RegistryKey registryKey4 = localMachine.OpenSubKey("SOFTWARE\\Microsoft\\Office\\16.0\\Excel\\InstallRoot\\");
			if (registryKey != null && File.Exists(registryKey.GetValue("Path").ToString() + "Excel.exe"))
			{
				num += 5;
			}
			if (registryKey2 != null && File.Exists(registryKey2.GetValue("Path").ToString() + "Excel.exe"))
			{
				num += 6;
			}
			if (registryKey3 != null && File.Exists(registryKey3.GetValue("Path").ToString() + "Excel.exe"))
			{
				num += 7;
			}
			if (registryKey4 != null && File.Exists(registryKey4.GetValue("Path").ToString() + "Excel.exe"))
			{
				num += 8;
			}
			return num;
		}

		private bool AddDropDownList(string FilePath, List<List<string>> columnsList)
		{
            //Microsoft.Office.Interop.Excel.Application application = (Microsoft.Office.Interop.Excel.Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
            //Microsoft.Office.Interop.Excel.Workbook workbook = application.Workbooks.Open(FilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            //for (int i = 0; i < workbook.Worksheets.Count; i++)
            //{
            //    if (GridToolExportImportExcel.<>o__10.<>p__0 == null)
            //    {
            //        GridToolExportImportExcel.<>o__10.<>p__0 = CallSite<Func<CallSite, object, Microsoft.Office.Interop.Excel.Worksheet>>.Create(Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(Microsoft.Office.Interop.Excel.Worksheet), typeof(GridToolExportImportExcel)));
            //    }
            //    Microsoft.Office.Interop.Excel.Worksheet worksheet = GridToolExportImportExcel.<>o__10.<>p__0.Target(GridToolExportImportExcel.<>o__10.<>p__0, workbook.Worksheets[i + 1]);
            //    string text = columnsList[i][0];
            //    columnsList[i].Remove(text);
            //    List<string> familyPictureName = this.GetFamilyPictureName(text);
            //    for (int j = 0; j < columnsList[i].Count; j++)
            //    {
            //        Microsoft.Office.Interop.Excel.Range range = worksheet.Columns.get_Range(columnsList[i][j] + ":" + columnsList[i][j], Type.Missing);
            //        range.Validation.Delete();
            //        range.Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, Type.Missing, familyPictureName[j], Type.Missing);
            //        worksheet.get_Range(columnsList[i][j] + "1", Type.Missing).Validation.Delete();
            //    }
            //}
            //workbook.Saved = true;
            //application.Visible = true;
			return true;
		}

		private List<string> GetFamilyPictureName(string sheetName)
		{
			List<string> list = new List<string>();
			string text = "";
			if (sheetName == "室内机")
			{
				text = "IndoorUnit";
			}
			else if (sheetName == "室外机")
			{
				text = "OutdoorUnit";
			}
			if (text != "")
			{
				string[] files = Directory.GetFiles(Path.Combine(Product.FamilyLocation, RevitVersion.Version, "VRV", text));
				string text2 = "";
				string text3 = "";
				string text4 = "";
				string[] array = files;
				for (int i = 0; i < array.Length; i++)
				{
					string text5 = new FileInfo(array[i]).Name.Split(new char[]
					{
						'.'
					})[0];
					string str = text5 + "_2D.png";
					string str2 = text5 + "_3D.png";
					text2 = text2 + str + ",";
					text3 = text3 + str2 + ",";
					text4 = text4 + text5 + ",";
				}
				text2 = text2.Substring(0, text2.Length - 1);
				text3 = text3.Substring(0, text3.Length - 1);
				text4 = text4.Substring(0, text4.Length - 1);
				list.Add(text2);
				list.Add(text3);
				list.Add(text4);
				return list;
			}
			return null;
		}

		private string ToName(int index)
		{
			List<string> list = new List<string>();
			do
			{
				if (list.Count > 0)
				{
					index--;
				}
				list.Insert(0, ((char)(index % 26 + 65)).ToString());
				index = (index - index % 26) / 26;
			}
			while (index > 0);
			return string.Join(string.Empty, list.ToArray());
		}

		private bool DataFromGridToExcel(Grid grid, List<string> head, List<List<string>> columnsList)
		{
			if (grid == null || this.m_book == null)
			{
				return false;
			}
			Sheet sheet = this.m_book.addSheet(head[0]);
			int rows = grid.Rows;
			int cols = grid.Cols;
			List<FlexCell.Range> list = new List<FlexCell.Range>();
			List<string> list2 = new List<string>();
			list2.Add(head[0]);
			for (int i = 1; i < cols - 1; i++)
			{
				sheet.setCol(i - 1, this.UnitTransform(grid.Column(i).Width, false));
				sheet.writeStr(0, i - 1, head[i + 2]);
				if (head[i + 2] == "俯视预览图片" || head[i + 2] == "三维真实预览图片" || head[i + 2] == "对应的族")
				{
					list2.Add(this.ToName(i - 1));
				}
			}
			columnsList.Add(list2);
			Format format = this.m_book.addFormat();
			for (int j = 1; j < rows; j++)
			{
				sheet.setRow(j - 1, this.UnitTransform(grid.Row(j).Height, true));
				int k = 1;
				while (k < cols - 1)
				{
					Cell cell = grid.Cell(j, k + 1);
					if (!cell.MergeCell)
					{
						goto IL_191;
					}
					FlexCell.Range range = grid.Range(j, k, j, k);
					if (!this.IfRangeExist(list, range))
					{
						list.Add(range);
						sheet.setMerge(range.FirstRow - 1, range.LastRow - 1, range.FirstCol - 1, range.LastCol - 1);
						goto IL_191;
					}
					IL_2EE:
					k++;
					continue;
					IL_191:
					string text = cell.ImageKey;
					if (text != "")
					{
						FlexCell.Image image = grid.Images.Item(text);
						if (string.IsNullOrEmpty(Path.GetExtension(text)))
						{
							text += ".jpg";
						}
						string text2 = Environment.GetEnvironmentVariable("TEMP");
						text2 = text2 + "\\" + text;
						image.SaveAs(text2, this.GetImageFormat(text));
						int pictureId = this.m_book.addPicture(text2);
						int width = image.Picture.Width;
						int height = image.Picture.Height;
						this.GetImageSize(image, cell.Alignment, grid.Column(k).Width, grid.Row(j).Height, ref width, ref height);
						sheet.setPicture(j - 1, k - 1, pictureId, width, height);
						File.Delete(text2);
						goto IL_2EE;
					}
					sheet.writeStr(j, k - 1, cell.Text, format);
					if (cell.MergeCell)
					{
						FlexCell.Range range2 = grid.Range(j, k, j, k);
						for (int l = range2.FirstRow; l <= range2.LastRow; l++)
						{
							for (int m = range2.FirstCol; m <= range2.LastCol; m++)
							{
								sheet.setCellFormat(l - 1, m - 1, format);
							}
						}
						goto IL_2EE;
					}
					goto IL_2EE;
				}
			}
			Format format2 = this.m_book.addFormat();
			format2.wrap = true;
			libxl.Font font = this.m_book.addFont();
			font.color = this.TransformColor(System.Drawing.Color.Red);
			format2.font = font;
			sheet.setMerge(rows, rows + 5, 0, 15);
			sheet.writeStr(rows, 0, head[1], format2);
			return true;
		}

		private bool DataFromGridToExcel(Grid grid)
		{
			if (grid == null || this.m_book == null)
			{
				return false;
			}
			Sheet sheet = this.m_book.addSheet("sheet1");
			int rows = grid.Rows;
			int cols = grid.Cols;
			List<FlexCell.Range> list = new List<FlexCell.Range>();
			for (int i = 1; i < cols; i++)
			{
				sheet.setCol(i - 1, this.UnitTransform(grid.Column(i).Width, false));
			}
			for (int j = 1; j < rows; j++)
			{
				sheet.setRow(j - 1, this.UnitTransform(grid.Row(j).Height, true));
				int k = 1;
				while (k < cols)
				{
					Cell cell = grid.Cell(j, k);
					if (!cell.MergeCell)
					{
						goto IL_F3;
					}
					FlexCell.Range range = grid.Range(j, k, j, k);
					if (!this.IfRangeExist(list, range))
					{
						list.Add(range);
						sheet.setMerge(range.FirstRow - 1, range.LastRow - 1, range.FirstCol - 1, range.LastCol - 1);
						goto IL_F3;
					}
					IL_384:
					k++;
					continue;
					IL_F3:
					string text = cell.ImageKey;
					if (text != "")
					{
						FlexCell.Image image = grid.Images.Item(text);
						if (string.IsNullOrEmpty(Path.GetExtension(text)))
						{
							text += ".jpg";
						}
						string text2 = Environment.GetEnvironmentVariable("TEMP");
						text2 = text2 + "\\" + text;
						image.SaveAs(text2, this.GetImageFormat(text));
						int pictureId = this.m_book.addPicture(text2);
						int width = image.Picture.Width;
						int height = image.Picture.Height;
						this.GetImageSize(image, cell.Alignment, grid.Column(k).Width, grid.Row(j).Height, ref width, ref height);
						sheet.setPicture(j - 1, k - 1, pictureId, width, height);
						File.Delete(text2);
						goto IL_384;
					}
					Format format = this.m_book.addFormat();
					if (cell.Text != "")
					{
						libxl.Font font = this.m_book.addFont();
						font.name = cell.Font.Name;
						font.size = Convert.ToInt32(cell.Font.Size);
						font.color = this.TransformColor(cell.ForeColor);
						font.bold = cell.Font.Bold;
						font.italic = cell.Font.Italic;
						font.strikeOut = cell.Font.Strikeout;
						font.underline = (cell.Font.Underline ? Underline.UNDERLINE_SINGLE : Underline.UNDERLINE_NONE);
						format.font = font;
						if (cell.WrapText)
						{
							format.wrap = true;
						}
					}
					libxl.Color color = this.TransformColor(cell.BackColor);
					if (color != libxl.Color.COLOR_WHITE)
					{
						format.patternForegroundColor = color;
						format.fillPattern = FillPattern.FILLPATTERN_SOLID;
					}
					this.SetAlignment(ref format, cell.Alignment);
					format.rotation = ((cell.Orientation == TextOrientationEnum.Horizontal) ? 0 : 90);
					this.SetBorderStyle(ref format, cell);
					sheet.writeStr(j - 1, k - 1, cell.Text, format);
					if (cell.MergeCell)
					{
						FlexCell.Range range2 = grid.Range(j, k, j, k);
						for (int l = range2.FirstRow; l <= range2.LastRow; l++)
						{
							for (int m = range2.FirstCol; m <= range2.LastCol; m++)
							{
								sheet.setCellFormat(l - 1, m - 1, format);
							}
						}
						goto IL_384;
					}
					goto IL_384;
				}
			}
			return true;
		}

		public bool ImportExcel(ref Grid grid, string FilePath)
		{
			if (grid == null || string.IsNullOrEmpty(FilePath))
			{
				return false;
			}
			if (!File.Exists(FilePath))
			{
				return false;
			}
			try
			{
				this.m_book.load(FilePath);
			}
			catch
			{
				YJKMessageBox.Show("未能正常打开文档，请检查后重新尝试");
				return false;
			}
			return this.DataFromExcelToGrid(ref grid);
		}

		public bool ImportExcel(Dictionary<string, Grid> grids, string FilePath)
		{
			if (grids == null || string.IsNullOrEmpty(FilePath))
			{
				return false;
			}
			if (!File.Exists(FilePath))
			{
				return false;
			}
			try
			{
				this.m_book.load(FilePath);
			}
			catch
			{
				YJKMessageBox.Show("未能正常打开文档，请检查后重新尝试");
				return false;
			}
			return this.DataFromExcelToGrid(grids);
		}

		private bool DataFromExcelToGrid(ref Grid grid)
		{
			if (this.m_book == null || grid == null)
			{
				return false;
			}
			Sheet sheet = this.m_book.getSheet(0);
			this.CreateNewGrid(sheet, ref grid);
			grid.AutoRedraw = false;
			for (int i = sheet.firstCol(); i < sheet.lastCol(); i++)
			{
				grid.Column(i + 1).Width = this.UnitTransform(sheet.colWidth(i), false);
			}
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			List<Rangex> list = new List<Rangex>();
			for (int j = sheet.firstRow(); j < sheet.lastRow(); j++)
			{
				grid.Row(j + 1).Height = this.UnitTransform(sheet.rowHeight(j), true);
				int k = sheet.firstCol();
				while (k < sheet.lastCol())
				{
					bool mergeX = sheet.getMergeX(j, k, ref num, ref num2, ref num3, ref num4);
					if (!mergeX)
					{
						goto IL_102;
					}
					Rangex rangex = new Rangex(num, num2, num3, num4);
					if (!this.IfRangeExist(list, rangex))
					{
						list.Add(rangex);
						grid.Range(num + 1, num3 + 1, num2 + 1, num4 + 1).Merge();
						goto IL_102;
					}
					IL_298:
					k++;
					continue;
					IL_102:
					Format format = sheet.cellFormat(j, k);
					string text = "";
					CellType cellType = sheet.cellType(j, k);
					if (cellType == CellType.CELLTYPE_BLANK || cellType == CellType.CELLTYPE_ERROR || cellType == CellType.CELLTYPE_EMPTY)
					{
						goto IL_298;
					}
					if (cellType == CellType.CELLTYPE_NUMBER)
					{
						text = sheet.readNum(j, k).ToString();
					}
					else if (cellType == CellType.CELLTYPE_STRING)
					{
						text = sheet.readStr(j, k);
					}
					else if (cellType == CellType.CELLTYPE_BOOLEAN)
					{
						text = sheet.readBool(j, k).ToString();
					}
					Cell cell = grid.Cell(j + 1, k + 1);
					cell.Text = text;
					cell.FontName = format.font.name;
					cell.FontSize = Convert.ToSingle(format.font.size);
					cell.ForeColor = this.TransformColor(format.font.color);
					cell.FontBold = format.font.bold;
					cell.FontItalic = format.font.italic;
					cell.FontStrikeout = format.font.strikeOut;
					cell.FontUnderline = (format.font.underline != Underline.UNDERLINE_NONE);
					cell.WrapText = format.wrap;
					cell.BackColor = this.TransformColor(format.patternForegroundColor);
					this.SetAlignment(format, ref cell);
					cell.Orientation = ((format.rotation == 0) ? TextOrientationEnum.Horizontal : TextOrientationEnum.Vertical);
					if (mergeX)
					{
						Format endFormat = sheet.cellFormat(num2, num4);
						this.SetBorderStyle(format, endFormat, ref cell);
						goto IL_298;
					}
					this.SetBorderStyle(format, ref cell);
					goto IL_298;
				}
			}
			for (int l = 0; l < sheet.pictureSize(); l++)
			{
				int num5 = 0;
				int num6 = 0;
				int num7 = 0;
				int num8 = 0;
				sheet.getPicture(l, ref num5, ref num6, ref num7, ref num8);
				if (num7 > grid.Rows - 1)
				{
					grid.Rows = num7 + 2;
					for (int m = sheet.lastRow() + 1; m < num7 + 2; m++)
					{
						grid.Row(m).Height = this.UnitTransform(sheet.rowHeight(m), true);
					}
				}
				if (num8 > grid.Cols - 1)
				{
					grid.Cols = num8 + 2;
					for (int n = sheet.lastCol() + 1; n < num8 + 2; n++)
					{
						grid.Column(n).Width = this.UnitTransform(sheet.colWidth(n), false);
					}
				}
				byte[] array = null;
				int num9 = 0;
				PictureType picture = this.m_book.getPicture(l, ref array, ref num9);
				if (array != null)
				{
					string text2 = this.SetImageFormat(picture);
					string text3 = l.ToString() + text2;
					string text4 = Environment.GetEnvironmentVariable("TEMP") + "\\" + text3;
					System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(array));
					image.Save(text4, this.GetImageFormat(text2));
					grid.Images.Add(text4, text3);
					grid.Cell(num5 + 1, num6 + 1).SetImage(text3);
					grid.Range(num5 + 1, num6 + 1, num7 + 1, num8 + 1).Merge();
					if (image != null)
					{
						image.Dispose();
					}
					File.Delete(text4);
				}
			}
			grid.AutoRedraw = true;
			grid.Refresh();
			return true;
		}

		private bool DataFromExcelToGrid(Dictionary<string, Grid> grids)
		{
			if (this.m_book == null || grids == null)
			{
				return false;
			}
			for (int i = 0; i < this.m_book.sheetCount(); i++)
			{
				Grid grid = new Grid();
				Sheet sheet = this.m_book.getSheet(i);
				this.CreateNewGrid(sheet, ref grid);
				grid.AutoRedraw = false;
				for (int j = sheet.firstCol(); j < sheet.lastCol(); j++)
				{
					grid.Column(j + 1).Width = this.UnitTransform(sheet.colWidth(j), false);
				}
				int num = 0;
				int num2 = 0;
				int num3 = 0;
				int num4 = 0;
				List<Rangex> list = new List<Rangex>();
				for (int k = sheet.firstRow(); k < sheet.lastRow(); k++)
				{
					grid.Row(k + 1).Height = this.UnitTransform(sheet.rowHeight(k), true);
					int l = sheet.firstCol();
					while (l < sheet.lastCol())
					{
						bool mergeX = sheet.getMergeX(k, l, ref num, ref num2, ref num3, ref num4);
						if (!mergeX)
						{
							goto IL_111;
						}
						Rangex rangex = new Rangex(num, num2, num3, num4);
						if (!this.IfRangeExist(list, rangex))
						{
							list.Add(rangex);
							grid.Range(num + 1, num3 + 1, num2 + 1, num4 + 1).Merge();
							goto IL_111;
						}
						IL_2A7:
						l++;
						continue;
						IL_111:
						Format format = sheet.cellFormat(k, l);
						string text = "";
						CellType cellType = sheet.cellType(k, l);
						if (cellType == CellType.CELLTYPE_BLANK || cellType == CellType.CELLTYPE_ERROR || cellType == CellType.CELLTYPE_EMPTY)
						{
							goto IL_2A7;
						}
						if (cellType == CellType.CELLTYPE_NUMBER)
						{
							text = sheet.readNum(k, l).ToString();
						}
						else if (cellType == CellType.CELLTYPE_STRING)
						{
							text = sheet.readStr(k, l);
						}
						else if (cellType == CellType.CELLTYPE_BOOLEAN)
						{
							text = sheet.readBool(k, l).ToString();
						}
						Cell cell = grid.Cell(k + 1, l + 1);
						cell.Text = text;
						cell.FontName = format.font.name;
						cell.FontSize = Convert.ToSingle(format.font.size);
						cell.ForeColor = this.TransformColor(format.font.color);
						cell.FontBold = format.font.bold;
						cell.FontItalic = format.font.italic;
						cell.FontStrikeout = format.font.strikeOut;
						cell.FontUnderline = (format.font.underline != Underline.UNDERLINE_NONE);
						cell.WrapText = format.wrap;
						cell.BackColor = this.TransformColor(format.patternForegroundColor);
						this.SetAlignment(format, ref cell);
						cell.Orientation = ((format.rotation == 0) ? TextOrientationEnum.Horizontal : TextOrientationEnum.Vertical);
						if (mergeX)
						{
							Format endFormat = sheet.cellFormat(num2, num4);
							this.SetBorderStyle(format, endFormat, ref cell);
							goto IL_2A7;
						}
						this.SetBorderStyle(format, ref cell);
						goto IL_2A7;
					}
				}
				for (int m = 0; m < sheet.pictureSize(); m++)
				{
					int num5 = 0;
					int num6 = 0;
					int num7 = 0;
					int num8 = 0;
					sheet.getPicture(m, ref num5, ref num6, ref num7, ref num8);
					if (num7 > grid.Rows - 1)
					{
						grid.Rows = num7 + 2;
						for (int n = sheet.lastRow() + 1; n < num7 + 2; n++)
						{
							grid.Row(n).Height = this.UnitTransform(sheet.rowHeight(n), true);
						}
					}
					if (num8 > grid.Cols - 1)
					{
						grid.Cols = num8 + 2;
						for (int num9 = sheet.lastCol() + 1; num9 < num8 + 2; num9++)
						{
							grid.Column(num9).Width = this.UnitTransform(sheet.colWidth(num9), false);
						}
					}
					byte[] array = null;
					int num10 = 0;
					PictureType picture = this.m_book.getPicture(m, ref array, ref num10);
					if (array != null)
					{
						string text2 = this.SetImageFormat(picture);
						string text3 = m.ToString() + text2;
						string text4 = Environment.GetEnvironmentVariable("TEMP") + "\\" + text3;
						System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(array));
						image.Save(text4, this.GetImageFormat(text2));
						grid.Images.Add(text4, text3);
						grid.Cell(num5 + 1, num6 + 1).SetImage(text3);
						grid.Range(num5 + 1, num6 + 1, num7 + 1, num8 + 1).Merge();
						if (image != null)
						{
							image.Dispose();
						}
						File.Delete(text4);
					}
				}
				grid.AutoRedraw = true;
				grid.Refresh();
				grids.Add(sheet.name, grid);
			}
			return true;
		}

		private ImageFormat GetImageFormat(string strImgKey)
		{
			string extension = Path.GetExtension(strImgKey);
			if (extension.Equals(".jpg", StringComparison.OrdinalIgnoreCase) || extension.Equals(".jpeg", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Jpeg;
			}
			if (extension.Equals(".png", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Png;
			}
			if (extension.Equals(".gif", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Gif;
			}
			if (extension.Equals(".icon", StringComparison.OrdinalIgnoreCase) || extension.Equals(".ico", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Icon;
			}
			if (extension.Equals(".tif", StringComparison.OrdinalIgnoreCase) || extension.Equals(".tiff", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Tiff;
			}
			if (extension.Equals(".bmp", StringComparison.OrdinalIgnoreCase))
			{
				return ImageFormat.Bmp;
			}
			return ImageFormat.Jpeg;
		}

		private string SetImageFormat(PictureType picType)
		{
			string result;
			if (picType != PictureType.PICTURETYPE_PNG)
			{
				if (picType != PictureType.PICTURETYPE_JPEG)
				{
					if (picType != PictureType.PICTURETYPE_TIFF)
					{
						result = ".jpg";
					}
					else
					{
						result = ".tif";
					}
				}
				else
				{
					result = ".jpg";
				}
			}
			else
			{
				result = ".png";
			}
			return result;
		}

		private double UnitTransform(short nPixel, bool bRow)
		{
			double num = Convert.ToDouble(nPixel);
			if (bRow)
			{
				num = num / this.m_dpiY * 2.54 * 28.6;
			}
			else
			{
				num = (num - 12.0) / 7.0 + 1.0;
			}
			return num;
		}

		private short UnitTransform(double dValue, bool bRow)
		{
			short result;
			if (bRow)
			{
				result = Convert.ToInt16(dValue / 28.6 / 2.54 * this.m_dpiY);
			}
			else
			{
				result = Convert.ToInt16((dValue - 1.0) * 7.0 + 12.0);
			}
			return result;
		}

		private libxl.Color TransformColor(System.Drawing.Color color)
		{
			return this.m_binBook.colorPack((int)color.R, (int)color.G, (int)color.B);
		}

		private System.Drawing.Color TransformColor(libxl.Color color)
		{
			if (color == libxl.Color.COLOR_DEFAULT_FOREGROUND)
			{
				return System.Drawing.Color.White;
			}
			if (color == (libxl.Color)(-1))
			{
				return System.Drawing.Color.Black;
			}
			int red = 0;
			int green = 0;
			int blue = 0;
			this.m_binBook.colorUnpack(color, ref red, ref green, ref blue);
			return System.Drawing.Color.FromArgb(red, green, blue);
		}

		private void SetAlignment(ref Format format, AlignmentEnum alignment)
		{
			if (format == null)
			{
				return;
			}
			switch (alignment)
			{
			case AlignmentEnum.LeftTop:
				format.alignH = AlignH.ALIGNH_LEFT;
				format.alignV = AlignV.ALIGNV_TOP;
				return;
			case AlignmentEnum.LeftCenter:
				format.alignH = AlignH.ALIGNH_LEFT;
				format.alignV = AlignV.ALIGNV_CENTER;
				return;
			case AlignmentEnum.LeftBottom:
				format.alignH = AlignH.ALIGNH_LEFT;
				format.alignV = AlignV.ALIGNV_BOTTOM;
				return;
			case AlignmentEnum.CenterTop:
				format.alignH = AlignH.ALIGNH_CENTER;
				format.alignV = AlignV.ALIGNV_TOP;
				return;
			case AlignmentEnum.CenterCenter:
				format.alignH = AlignH.ALIGNH_CENTER;
				format.alignV = AlignV.ALIGNV_CENTER;
				return;
			case AlignmentEnum.CenterBottom:
				format.alignH = AlignH.ALIGNH_CENTER;
				format.alignV = AlignV.ALIGNV_BOTTOM;
				return;
			case AlignmentEnum.RightTop:
				format.alignH = AlignH.ALIGNH_RIGHT;
				format.alignV = AlignV.ALIGNV_TOP;
				return;
			case AlignmentEnum.RightCenter:
				format.alignH = AlignH.ALIGNH_RIGHT;
				format.alignV = AlignV.ALIGNV_CENTER;
				return;
			case AlignmentEnum.RightBottom:
				format.alignH = AlignH.ALIGNH_RIGHT;
				format.alignV = AlignV.ALIGNV_BOTTOM;
				return;
			}
			format.alignH = AlignH.ALIGNH_CENTER;
			format.alignV = AlignV.ALIGNV_CENTER;
		}

		private void SetAlignment(Format format, ref Cell cell)
		{
			if (format == null || cell == null)
			{
				return;
			}
			AlignH alignH = format.alignH;
			AlignV alignV = format.alignV;
			if (format.alignH == AlignH.ALIGNH_LEFT && format.alignV == AlignV.ALIGNV_TOP)
			{
				cell.Alignment = AlignmentEnum.LeftTop;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_LEFT && format.alignV == AlignV.ALIGNV_CENTER)
			{
				cell.Alignment = AlignmentEnum.LeftCenter;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_LEFT && format.alignV == AlignV.ALIGNV_BOTTOM)
			{
				cell.Alignment = AlignmentEnum.LeftBottom;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_CENTER && format.alignV == AlignV.ALIGNV_TOP)
			{
				cell.Alignment = AlignmentEnum.CenterTop;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_CENTER && format.alignV == AlignV.ALIGNV_CENTER)
			{
				cell.Alignment = AlignmentEnum.CenterCenter;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_CENTER && format.alignV == AlignV.ALIGNV_BOTTOM)
			{
				cell.Alignment = AlignmentEnum.CenterBottom;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_RIGHT && format.alignV == AlignV.ALIGNV_TOP)
			{
				cell.Alignment = AlignmentEnum.RightTop;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_RIGHT && format.alignV == AlignV.ALIGNV_CENTER)
			{
				cell.Alignment = AlignmentEnum.RightCenter;
				return;
			}
			if (format.alignH == AlignH.ALIGNH_RIGHT && format.alignV == AlignV.ALIGNV_BOTTOM)
			{
				cell.Alignment = AlignmentEnum.RightBottom;
				return;
			}
			cell.Alignment = AlignmentEnum.CenterCenter;
		}

		private void SetBorderStyle(ref Format format, Cell cell)
		{
			if (format == null || cell == null)
			{
				return;
			}
			LineStyleEnum lineStyleEnum = cell.get_Border(EdgeEnum.Left);
			if (lineStyleEnum == LineStyleEnum.Thick)
			{
				format.borderLeft = BorderStyle.BORDERSTYLE_THICK;
			}
			else if (lineStyleEnum == LineStyleEnum.Thin)
			{
				format.borderLeft = BorderStyle.BORDERSTYLE_THIN;
			}
			lineStyleEnum = cell.get_Border(EdgeEnum.Right);
			if (lineStyleEnum == LineStyleEnum.Thick)
			{
				format.borderRight = BorderStyle.BORDERSTYLE_THICK;
			}
			else if (lineStyleEnum == LineStyleEnum.Thin)
			{
				format.borderRight = BorderStyle.BORDERSTYLE_THIN;
			}
			lineStyleEnum = cell.get_Border(EdgeEnum.Top);
			if (lineStyleEnum == LineStyleEnum.Thick)
			{
				format.borderTop = BorderStyle.BORDERSTYLE_THICK;
			}
			else if (lineStyleEnum == LineStyleEnum.Thin)
			{
				format.borderTop = BorderStyle.BORDERSTYLE_THIN;
			}
			lineStyleEnum = cell.get_Border(EdgeEnum.Bottom);
			if (lineStyleEnum == LineStyleEnum.Thick)
			{
				format.borderBottom = BorderStyle.BORDERSTYLE_THICK;
			}
			else if (lineStyleEnum == LineStyleEnum.Thin)
			{
				format.borderBottom = BorderStyle.BORDERSTYLE_THIN;
			}
			if (cell.get_Border(EdgeEnum.DiagonalUp) != LineStyleEnum.None && cell.get_Border(EdgeEnum.DiagonalDown) == LineStyleEnum.None)
			{
				format.borderStyle = BorderStyle.BORDERSTYLE_THIN;
				format.borderDiagonal = BorderDiagonal.BORDERDIAGONAL_UP;
			}
			else if (cell.get_Border(EdgeEnum.DiagonalUp) == LineStyleEnum.None && cell.get_Border(EdgeEnum.DiagonalDown) != LineStyleEnum.None)
			{
				format.borderStyle = BorderStyle.BORDERSTYLE_THIN;
				format.borderDiagonal = BorderDiagonal.BORDERDIAGONAL_DOWN;
			}
			if (cell.get_Border(EdgeEnum.DiagonalUp) != LineStyleEnum.None && cell.get_Border(EdgeEnum.DiagonalDown) != LineStyleEnum.None)
			{
				format.borderStyle = BorderStyle.BORDERSTYLE_THIN;
				format.borderDiagonal = BorderDiagonal.BORDERDIAGONAL_BOTH;
			}
		}

		private void SetBorderStyle(Format format, ref Cell cell)
		{
			if (format == null || cell == null)
			{
				return;
			}
			BorderStyle borderStyle = format.borderLeft;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Left, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Left, LineStyleEnum.Thin);
			}
			borderStyle = format.borderRight;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Right, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Right, LineStyleEnum.Thin);
			}
			borderStyle = format.borderTop;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Top, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Top, LineStyleEnum.Thin);
			}
			borderStyle = format.borderBottom;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Bottom, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Bottom, LineStyleEnum.Thin);
			}
			switch (format.borderDiagonal)
			{
			case BorderDiagonal.BORDERDIAGONAL_DOWN:
				cell.set_Border(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			case BorderDiagonal.BORDERDIAGONAL_UP:
				cell.set_Border(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				return;
			case BorderDiagonal.BORDERDIAGONAL_BOTH:
				cell.set_Border(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				cell.set_Border(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			default:
				return;
			}
		}

		private void SetBorderStyle(Format startFormat, Format endFormat, ref Cell cell)
		{
			if (startFormat == null || endFormat == null || cell == null)
			{
				return;
			}
			BorderStyle borderStyle = startFormat.borderLeft;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Left, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Left, LineStyleEnum.Thin);
			}
			borderStyle = startFormat.borderTop;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Top, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Top, LineStyleEnum.Thin);
			}
			borderStyle = endFormat.borderRight;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Right, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Right, LineStyleEnum.Thin);
			}
			borderStyle = endFormat.borderBottom;
			if (borderStyle == BorderStyle.BORDERSTYLE_THICK || borderStyle == BorderStyle.BORDERSTYLE_MEDIUM)
			{
				cell.set_Border(EdgeEnum.Bottom, LineStyleEnum.Thick);
			}
			else if (borderStyle == BorderStyle.BORDERSTYLE_THIN)
			{
				cell.set_Border(EdgeEnum.Bottom, LineStyleEnum.Thin);
			}
			switch (startFormat.borderDiagonal)
			{
			case BorderDiagonal.BORDERDIAGONAL_DOWN:
				cell.set_Border(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			case BorderDiagonal.BORDERDIAGONAL_UP:
				cell.set_Border(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				return;
			case BorderDiagonal.BORDERDIAGONAL_BOTH:
				cell.set_Border(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				cell.set_Border(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			default:
				return;
			}
		}

		private void CreateNewGrid(Sheet sheet, ref Grid grid)
		{
			grid.NewFile();
			grid.DisplayRowNumber = true;
			grid.ShowResizeTip = true;
			grid.Rows = sheet.lastRow() + 1;
			grid.Cols = sheet.lastCol() + 1;
			for (int i = 1; i < grid.Cols; i++)
			{
				grid.Cell(0, i).Text = i.ToString();
			}
		}

		private bool IfRangeExist(List<FlexCell.Range> lstRange, FlexCell.Range range)
		{
			foreach (FlexCell.Range range2 in lstRange)
			{
				if (range2.FirstRow == range.FirstRow && range2.FirstCol == range.FirstCol && range2.LastRow == range.LastRow && range2.LastCol == range.LastCol)
				{
					return true;
				}
			}
			return false;
		}

		private bool IfRangeExist(List<Rangex> lstRange, Rangex range)
		{
			foreach (Rangex rangex in lstRange)
			{
				if (rangex.RowFirst == range.RowFirst && rangex.ColFirst == range.ColFirst && rangex.RowLast == range.RowLast && rangex.ColLast == range.ColLast)
				{
					return true;
				}
			}
			return false;
		}

		private void GetImageSize(FlexCell.Image image, AlignmentEnum cellAlign, short cellWidth, short cellHeight, ref int imgWidth, ref int imgHeight)
		{
			if (image == null)
			{
				return;
			}
			if (cellAlign != AlignmentEnum.CenterCenter)
			{
				imgWidth = (int)cellWidth;
				imgHeight = (int)cellHeight;
				return;
			}
			if (Geometry.GreaterThan((double)cellWidth, (double)imgWidth) && Geometry.GreaterThan((double)cellHeight, (double)imgHeight))
			{
				return;
			}
			if (Geometry.GreaterThan((double)cellWidth, (double)cellHeight))
			{
				imgWidth = (int)cellHeight * imgWidth / imgHeight;
				imgHeight = (int)cellHeight;
				return;
			}
			imgHeight = (int)cellWidth * imgHeight / imgWidth;
			imgWidth = (int)cellWidth;
		}

		private Book m_binBook = new BinBook();

		private Book m_xmlBook = new XmlBook();

		private Book m_book;

		private double m_dpiX;

		private double m_dpiY;
	}
}
